package arr

// 使用iteration（迭代函数）将array中的所有元素进行排序

func OrderBy(a []int, itr func (x, y int) bool) []int {
	for i := 0; i < len(a); i++ {
		for j := 1 + i; j < len(a); j++ {
			if itr(a[i], a[j]) {
				a[i], a[j] = a[j], a[i]
			}
		}
	}
	return a
}

func OrderByInt64(a []int64, itr func (x, y int64) bool) []int64 {
	for i := 0; i < len(a); i++ {
		for j := 1 + i; j < len(a); j++ {
			if itr(a[i], a[j]) {
				a[i], a[j] = a[j], a[i]
			}
		}
	}
	return a
}

func OrderByUInt(a []uint, itr func (x, y uint) bool) []uint {
	for i := 0; i < len(a); i++ {
		for j := 1 + i; j < len(a); j++ {
			if itr(a[i], a[j]) {
				a[i], a[j] = a[j], a[i]
			}
		}
	}
	return a
}

func OrderByUInt64(a []uint64, itr func (x, y uint64) bool) []uint64 {
	for i := 0; i < len(a); i++ {
		for j := 1 + i; j < len(a); j++ {
			if itr(a[i], a[j]) {
				a[i], a[j] = a[j], a[i]
			}
		}
	}
	return a
}

func OrderByFloat(a []float64, itr func (x, y float64) bool) []float64 {
	for i := 0; i < len(a); i++ {
		for j := 1 + i; j < len(a); j++ {
			if itr(a[i], a[j]) {
				a[i], a[j] = a[j], a[i]
			}
		}
	}
	return a
}
